package com.ace3l.legal.gwt.domainmodule.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ace3l.legal.gwt.domainmodule.client.StudentPortalService;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTStudent;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTProject;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTRole;
import com.ace3l.legal.gwt.domainmodule.domainxform.StudentXForm;
import com.ace3l.legal.gwt.domainmodule.domainxform.ProjectXForm;
import com.ace3l.legal.backend.dao.StudentDao;
import com.ace3l.legal.backend.dao.ProjectDao;
import com.ace3l.legal.backend.domain.User;
import com.ace3l.legal.backend.domain.Student;
import com.ace3l.legal.backend.domain.Project;
import com.ace3l.legal.Constants;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 31, 2008
 * Time: 2:11:26 AM
 * To change this template use File | Settings | File Templates.
 */
public class StudentPortalServiceImpl extends RemoteServiceServlet implements StudentPortalService {
    public GWTStudent getAbridgedStudent() {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        StudentDao studentDao = (StudentDao)appCtx.getBean("studentDao");

        User myProfile = (User)this.getThreadLocalRequest().getSession().getAttribute(Constants.USERPROFILE);
        Student myStudent = studentDao.getStudentAbridgedById(myProfile.getId());
        return StudentXForm.toGWT(myStudent);
    }

    public String[] getAbridgedProjectHeader() {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProjectDao projectDao = (ProjectDao)appCtx.getBean("projectDao");
        return projectDao.getProjectsByStatusAbridgedHeader();
    }
    
    public String getAbridgedNewProjects() {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProjectDao projectDao = (ProjectDao)appCtx.getBean("projectDao");
        List<Object[]> ret = (List<Object[]>)projectDao.getProjectsByStatus("NEW", true, ProjectDao.RESULT_FORMAT_LIST_OF_ARRAY, true);
        XStream xstream = new XStream(new JsonHierarchicalStreamDriver());

        String jsonString = xstream.toXML(ret);
        System.out.println(jsonString);
        return jsonString;
    }

    public GWTProject getProject(String projectId) throws Exception
    {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProjectDao projectDao = (ProjectDao)appCtx.getBean("projectDao");

        Project myProject = Project.EMPTY_PROJECT;
        try {
            myProject = projectDao.getProject(Integer.parseInt(projectId));
        } catch (Exception e) {}
        GWTProject retProject = ProjectXForm.toGWT(myProject);
        retProject.secureForRole(GWTRole.STUDENT);
        return retProject;
    }
}